package operation.duoxianliu.demo03;

public class Dianyp implements Runnable {
    private static int sradc = 1000;

    @Override
    public void run() {
        while (true) {
            synchronized (Dianyp.class) {
                if ("一号窗口".equals(Thread.currentThread().getName())) {
                    boolean srws = srws();
                    if (srws) {
                        break;
                    }
                }
                if ("二号窗口".equals(Thread.currentThread().getName())) {
                    if (sradc == 0) {
                        break;
                    }try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    sradc--;
                    System.out.println(Thread.currentThread().getName() + "电影票还剩下" + sradc + "张");
                }
            }
        }

    private static synchronized boolean srws() {
        while (true){
        if (sradc==0) {
            return true;
        }else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sradc--;
            System.out.println(Thread.currentThread().getName()+"电影票还剩下"+sradc+"张");
        }
        return false;
    }
}
}